package com.ljx.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ljx.Domain.AddressBook;
import com.ljx.Domain.MyThreadLocal;
import com.ljx.Domain.R;
import com.ljx.Service.AddressBookService;
import com.sun.prism.impl.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("addressBook")
public class AddressBookController {
    @Autowired
    AddressBookService addressBookService;

    /**
     * 地址菜单
     * @return
     */
    @GetMapping("list")
    public R<List<AddressBook>> addressBookList(HttpSession session){
        //根据id查询
        String user = session.getAttribute("user").toString();
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        //添加条件
        wrapper.eq(AddressBook::getUserId,user);
        wrapper.orderByDesc(AddressBook::getUpdateTime);
        //根据id查询数据库
        List<AddressBook> list = addressBookService.list(wrapper);
        return R.success(list);
    }

    /**
     * 保存地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook,HttpSession session){
        //添加用户id
        Long userId = Long.parseLong(session.getAttribute("user").toString());
        addressBook.setUserId(userId);
        //保存地址
        addressBookService.save(addressBook);

        return R.success("保存成功");
    }

    /**
     * 结算页面返回默认地址
     * @return
     */
    @GetMapping("default")
    public R<AddressBook> defaultAddress(){
        //根据用户id和isDefault=1查询默认地址
        Long userId = MyThreadLocal.getThreadLocal();
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId,userId);
        wrapper.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookService.getOne(wrapper);
        return R.success(addressBook);
    }

    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> updateAddress(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("更新成功");
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("default")
    public R<String> addressBookDefault(@RequestBody AddressBook addressBook){
        //第一步，取消该用户的全部地址
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId,MyThreadLocal.getThreadLocal());
        System.out.println(MyThreadLocal.getThreadLocal());
        wrapper.set(AddressBook::getIsDefault,0);
        addressBookService.update(wrapper);
        //第二步，设置新的默认地址
        LambdaUpdateWrapper<AddressBook> wrapperUpdate = new LambdaUpdateWrapper<>();
        wrapperUpdate.eq(AddressBook::getId,addressBook.getId());
        wrapperUpdate.set(AddressBook::getIsDefault,1);
        addressBookService.update(wrapperUpdate);
        return R.success("更改成功");
    }
    /**
     * 根据id查询地址
     */
    @GetMapping("/{id}")
    public R get(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook != null) {
            return R.success(addressBook);
        } else {
            return R.error("没有找到该对象");
        }
    }


}
